<template>
  <van-icon
    color="#777"
    :name="attitude===1 ? 'good-job' : 'good-job-o'"
    :class="attitude === 1 ? 'yellow' : ''"
    @click="handelLike"
  />
</template>

<script>
import { likeArticle, UnLikeArticle } from '@/api/articles'
export default {
  name: 'LikeArticle',
  model: {
    prop: 'attitude',
    event: 'changeAttitude'
  },
  props: {
    attitude: {
      type: Number,
      required: true
    },
    articleId: {
      type: [Number, String, Object],
      required: true
    }
  },
  methods: {
    async handelLike () {
      try {
        let status = -1
        if (this.attitude === 1) { // 被收藏了，点击后取消收藏 颜色变黑
          await UnLikeArticle(this.articleId)
        } else { // 没被收藏 点击后收藏
          await likeArticle(this.articleId)
          status = 1
        }
        this.$emit('changeAttitude', status)
        this.$toast.success(status === 1 ? '点赞成功' : '取消点赞')
      } catch (err) {
        this.$toast.fail('操作失败，请稍后重试')
      }
    }
  }
}
</script>

<style scoped lang="less">
  .yellow {
    color: #ffa500 !important;
  }
</style>
